package com.hfzy.ihk.web.websocket.support.toApp;

import com.hfzy.ihk.common.util.http.WebUtil;
import com.hfzy.ihk.common.util.string.StringUtil;
import com.hfzy.ihk.web.websocket.conf.PublicConfig;
import com.hfzy.ihk.web.websocket.constant.Constants;
import com.hfzy.ihk.web.websocket.support.WebSocketManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * Created by Administrator on 2016/11/20.
 *
 * @author wws
 */
public class AppWebSocketHandshakeInterceptor implements HandshakeInterceptor {

    private static final Logger logger = LoggerFactory.getLogger(AppWebSocketHandshakeInterceptor.class);

    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {

        try {
            HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
            String userId = servletRequest.getParameter("userId");
            String token  = servletRequest.getParameter("token");//token应当用于安全验证，暂留
            String cardId  = servletRequest.getParameter("cardId");
            String phoneMac  = servletRequest.getParameter("phoneMac");
            String mianCallPhone  = servletRequest.getParameter("mianCallPhone");
            String appVersion  = servletRequest.getParameter("appVersion");

            if(Constants.SYSTEM_ENV_PRO.equals(System.getProperty("spring.profiles.active")) &&
                    (StringUtil.isEmpty(userId) || !StringUtil.isNumeric(userId)))
                return false;

	        /*---------防止测试时，与前线的帐号冲突----------*/
            String ip = WebUtil.getIpAddr(((ServletServerHttpRequest) request).getServletRequest());

            boolean isTest;

            if(Constants.SYSTEM_ENV_PRO.equals(System.getProperty("spring.profiles.active")))
                isTest = PublicConfig.CLOUDCALL_V2_TEST_IP_PRO.equals(ip);
            else
                isTest = PublicConfig.CLOUDCALL_V2_TEST_IP_DEV.contains(ip);

            if(isTest){
                attributes.put("isTest", Constants.TRUE);
                attributes.put("userId", WebSocketManager.createTestUserId(userId));
            }else {
                attributes.put("userId",userId);
            }

            attributes.put("cardId",cardId);
            attributes.put("phoneMac",phoneMac);
            attributes.put("connectTime",new Date());
            attributes.put("isBusy",false);
            attributes.put("appVersion",StringUtil.isEmpty(appVersion)?"0":appVersion);

            if(StringUtil.isNotNull(token))
                attributes.put("token",token);
            if(StringUtil.isNotNull(mianCallPhone))
                attributes.put("mianCallPhone",mianCallPhone);

            attributes.put("currentCallBrowserSocketInfo",Constants.null_str);

//        WebSocketManager.saveAppOnLineInfo(userId,token,cardId,mianCallPhone,phoneMac, Constants.SUCCESS_1);

            logger.info("app handshake ----》userId:{},phoneMac:{},cardId:{},mianCallPhone:{},token:{},appVersion:{}"
                    ,userId,phoneMac,cardId,mianCallPhone,token,appVersion);

            return true;

        } catch (Exception e) {
            logger.error("websocket握手失败:{}",e);
            return false;
        }
    }

    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
    }

}
